package com.osi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osi.bean.Course;
import com.osi.bean.Logininfo;
import com.osi.bean.Student;
import com.osi.bean.StudentCourse;
import com.osi.response.R;
import com.osi.mapper.StudentCourseMapper;
import com.osi.service.CourseService;
import com.osi.service.StudentCourseService;
import com.osi.service.StudentService;
import com.osi.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lixin
 * @date 2021/2/8 16:13
 */
@Service
public class StudentCourseServiceImpl extends ServiceImpl<StudentCourseMapper, StudentCourse> implements StudentCourseService {

    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseService courseService;



    /**
     * 学生选课
     * @param courseId
     */
    @Override
    public R selectCourse(Integer courseId) {
        Logininfo logininfo = UserContext.getCurreentUser();
        Student student = studentService.getOne(new QueryWrapper<Student>()
                .eq("username", logininfo.getUsername()));

        Course course = courseService.getById(courseId);

        if (course.getSelectCount() >= course.getTotalCount()) {
            return R.error("该课程人数已满");
        }

        StudentCourse studentCourse = baseMapper.selectOne(new QueryWrapper<StudentCourse>()
                .eq("student_id", student.getId())
                .eq("course_id", course.getCourseId())
        );
        if (ObjectUtil.isNotNull(studentCourse)) {
            return R.error("你已选过该课程");
        }

        // 添加选课记录
        studentCourse = new StudentCourse();
        studentCourse.setStudentId(student.getId());
        studentCourse.setCourseId(course.getCourseId());
        baseMapper.insert(studentCourse);

        // 更新课程人员信息
        course.setSelectCount(course.getSelectCount() + 1);
        courseService.updateById(course);
        return R.ok("选课成功，请在我的选课中查看");
    }


    /**
     * 学生退课
     * @param courseId
     */
    @Override
    public R quitCourse(Integer courseId) {
        Logininfo logininfo = UserContext.getCurreentUser();
        Student student = studentService.getOne(new QueryWrapper<Student>()
                .eq("username", logininfo.getUsername()));

        Course course = courseService.getById(courseId);



        // 删除选课信息
        StudentCourse studentCourse = baseMapper.selectOne(new QueryWrapper<StudentCourse>()
                .eq("student_id", student.getId())
                .eq("course_id", course.getCourseId())
        );
        if (ObjectUtil.isNotNull(studentCourse)) {
            baseMapper.delete(new QueryWrapper<StudentCourse>()
                    .eq("student_id", student.getId())
                    .eq("course_id", course.getCourseId()));

        }

        // 更新课程人员信息
        course.setSelectCount(course.getSelectCount() - 1);
        courseService.updateById(course);

        return R.ok("退课成功，请重新查询");

    }
}

